Ame Liu 22910358
library(ggplot2)
library(ggsci)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
library(ggplot2)
library(gridExtra)
library(dplyr)
library(ggthemes)
library(numform)
##
## Attaching package: 'numform'
## The following object is masked from 'package:dplyr':
##
## collapse
library(treemapify)
library(timeDate)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(dplyr)
library(reshape2)
library(ca)
1.pairs 矩阵散点图 探索两两变量之间的相关性
rw <-read.table('/Users/liuyu/Downloads/winequality-red.csv', head=T,sep=',',fileEncoding = 'UTF-8')
pairs(rw)
pairs(~pH+alcohol+quality+density, data=rw)
只显示下半边数据
pairs(~pH+alcohol+quality+density, data=rw,upper.panel = NULL)
绘制mpg图像
a <- data.frame(mpg)
pairs(~displ+cty+hwy, data=mpg)# mpg是ggplot2的内置数据集
library(ggplot2)
summary(mpg$displ)#mpg的displ数据
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.600 2.400 3.300 3.472 4.600 7.000
ggplot(iris,aes(Sepal.Length,Sepal.Width))+geom_point()+facet_wrap(~Species)#作iris数据集的图
#针对iris数据库进行关于Species的分面处理
ggplot(mpg,aes(displ,hwy))+geom_point(colour='blue')+facet_wrap(~class,ncol = 2)+labs(title = "point geom")#aes是设置横纵坐标,facet_wrap此处为封装型,geom_point()是绘制散点图,tile是设置标题.
ggplot(mpg,aes(displ,hwy))+geom_point(colour='blue')+labs(title = "point geom")#点状图
ggplot(mpg,aes(displ,hwy))+geom_smooth(colour='yellow')+labs(title = "geom_smooth")#平滑图
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#通过gridExtra做对比图
p1 <- ggplot(mpg,aes(displ,hwy))+geom_point(colour='blue')+facet_wrap(~class)+labs(title = "point geom")
p2 <- ggplot(mpg,aes(displ,hwy))+geom_smooth(colour='yellow')+labs(title = "geom_smooth")
library(gridExtra)
grid.arrange(p1, p2, ncol=2)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
展示一下mpg和iris内置数据集,是流行的车的数据
rrr <- data.frame(mpg)
rrr
mm <- data.frame(iris)
mm
Aesthetic levels
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, size = class), show.legend = TRUE)
## Warning: Using size for a discrete variable is not advised.
d<- ggplot(mpg,aes(x=displ,y=hwy,size = class))+geom_point(fill = "red",show.legend = TRUE)+labs(title = "point geom") #geom_point1
c <- ggplot(mpg,aes(x=displ,y=hwy,shape = drv,alpha = class))+geom_point(colour='black',fill = "red")+labs(title = "point geom") #geom_point2
a <- ggplot(mpg,aes(x=displ,y=hwy,group = drv))+geom_smooth(colour='yellow')+labs(title = "geom_smooth")
#geom_smooth1
b <- ggplot(mpg,aes(x=displ,y=hwy,color = drv,linetype = drv))+geom_smooth(colour='yellow')+labs(title = "geom_smooth") #geom_smooth2
grid.arrange(a, b,c, d, ncol=2)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Using alpha for a discrete variable is not advised.
## Warning: Using size for a discrete variable is not advised.
library(gridExtra)
p1 <- ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, group = drv)) #mapping意为映射
p2 <- ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, color = drv,
linetype = drv))
p3 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, size = class),
show.legend = TRUE) #显示图例标注
p4 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, alpha = class,
shape = drv))
grid.arrange(p1, p2, p3, p4, ncol=2)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Using size for a discrete variable is not advised.
## Warning: Using alpha for a discrete variable is not advised.
ggplot(mpg,aes(x = displ, y = hwy))+geom_point()+geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#作图(数据,坐标)+ 选择图形种类
ggplot(mpg,aes(x=displ,y=hwy,color=class))+geom_point()+geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : span too small. fewer data values than degrees of freedom.
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : pseudoinverse used at 5.6935
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : neighborhood radius 0.5065
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : reciprocal condition number 0
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : There are other near singularities as well. 0.65044
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : span too small. fewer
## data values than degrees of freedom.
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at
## 5.6935
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
## 0.5065
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
## number 0
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : There are other near
## singularities as well. 0.65044
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : pseudoinverse used at 4.008
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : neighborhood radius 0.708
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : reciprocal condition number 0
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : There are other near singularities as well. 0.25
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at
## 4.008
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
## 0.708
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
## number 0
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : There are other near
## singularities as well. 0.25
#颜色作为种类的依据
Hexbin - 2D histogram geom_hex()为六边形散点图
library(hexbin)
a1 <- with(rw,{bin<-hexbin(x=pH,y=density,xbins=10) #xbins的意思为x即是个数,以10个位一组计数
plot(bin)})
ggplot(rw,aes(pH,density))+geom_smooth(colour='yellow')+labs(title = "geom_smooth")+geom_hex()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
#geom_hax()做六边形散点图
ggplot2之常见观察变量绘图geom https://www.jianshu.com/p/4e4f6168fa37
反映频率的图(适合整数):
ggplot(data = rw) +
geom_count(mapping = aes(x=pH, y=density))
近似热图
library(dplyr)
counting <- count(mpg, displ, hwy)
ggplot(data = counting, mapping = aes(x = displ,y = hwy)) +
geom_tile(mapping = aes(fill = n))#设n为数量
适用于这种带类型的变量
b <- data.frame(diamonds)
b
library(ggplot2)
ggplot(data = diamonds) +
stat_summary(
mapping = aes(x = cut, y = depth),
fun.ymin = min,
fun.ymax = max,
fun.y = median
)
## Warning: `fun.y` is deprecated. Use `fun` instead.
## Warning: `fun.ymin` is deprecated. Use `fun.min` instead.
## Warning: `fun.ymax` is deprecated. Use `fun.max` instead.
箱型图的使用
p1 <- ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot()
p2 <- ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()
grid.arrange(p1, p2, ncol=2)
Polar coordinate 极坐标的使用
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
p1 <- bar + coord_flip()
p2 <- bar + coord_polar()
grid.arrange(p1, p2, ncol=2)
地图坐标的使用Map Coordinates coord_quickmap()
a <- data.frame(map_data("world"))
a
library(maps)
w <- map_data("world")
p1 <- ggplot(w, aes(long, lat, group = group)) +
geom_polygon(fill = "white", colour = "black")
p2 <- ggplot(w, aes(long, lat, group = group)) +
geom_polygon(fill = "white", colour = "black") +
coord_quickmap()
grid.arrange(p1, p2, ncol=2)